import prompt from '@/components/Prompt';
import {
  selectEndorseList,
  selectEndorseDetail,
  endorseOperate,
  deleteEndorse,
  confirmEndorse,
  commitEndorse,
  exportEndorse,
} from '@/services/endorse';

export default {
  namespace: 'Endorse',
  state: {
    endorseList: {}, // 帐单列表
    endorseDetail: {},
   // archivesDetail: {},
    formValues: {},
    oilCardList: {},
    oilInfoDetails: {},
    oilInfoList: {},
    oilBindInfoList: {},
    incidentalInfoList: {},
    trainInfoList: {},
    trainInfoDetails: {},
    pressureInfoList: {},
    feeData: {}, //费用数据
    emptyDetails: {},
  },
//Effect :Action 处理器，处理异步动作，基于 Redux-saga 实现。
  effects: {
    // 一、违章记录
    // 1、违章记录  列表

    *selectEndorseList({ payload, callback }, { call, put }) {
      const response = yield call(selectEndorseList, payload);
      console.log('------response------------',response);
      
      const { code, data } = response;
      if (code !== 0) return;
      const { list, pageSize, total, pageNum } = data;
      yield put({
        type: 'saveAllValus',
        payload: {
          endorseList: {
            pagination: {
              current: pageNum,
              pageSize,
              total,
            },
            list,
          },
        },
      });
      callback && callback(list);
    },
    // 2、违章记录  详情
    *selectEndorseDetail({ payload, callback }, { call, put }) {
      const response = yield call(selectEndorseDetail, payload);
      const { code, data } = response;
      if (code !== 0) return;
      let newData = data;
      
      yield put({
        type: 'detail',
        payload: { endorseDetail: { [payload.id]: newData } },
      });
      callback && callback(newData);
    },


    // 3、违章记录  删除
    *deleteEndorse({ payload, callback }, { call }) {
      const response = yield call(deleteEndorse, payload);
      const { code, data, message } = response;
      const content = `删除${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },

    // 4、违章记录  操作：新增、编辑 archivesOperate
    *endorseOperate({ payload, callback }, { call }) {
      const response = yield call(endorseOperate, payload);
      const { code, data, message } = response;
      const content = payload.id ? `编辑${message}` : `新增${message}`;
      if (code !== 0) return;
      if (payload.operateType == 'save') {
        prompt({ content });
      }
      callback && callback(data);
    },

    
    // 5、违章记录  确认
    *confirmEndorse({ payload, callback }, { call }) {
      const response = yield call(confirmEndorse, payload);
      const { code, data, message } = response;
      const content = `确认${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },

    //6 违章记录  导出 exportEndorse
   
    *exportEndorse({ payload, callback }, { call }) {
       yield call(exportEndorse, payload);
    },


    // 6、行车档案  试算
    *trialFeeArchives({ payload, callback }, { call }) {
      const response = yield call(trialFeeArchives, payload);
      const { code, data, message } = response;
      const content = `试算${message}`;
      if (code !== 0) return;
      if (payload.type == 'trial') prompt({ content });
      callback && callback(data);
      //feeData 用于存储已经试算的行车档案，当点击提交的时候，
      // 验证法1：可以通过这个判断是否已试算，如果已试算，则直接提交，否则就先试算再提交
      //验证法2：通过返回的行车档案详情， driverBillList 字段是否有值来判断
      yield put({
        type: 'detail',
        payload: { feeData: { [payload.id]: data } },
      });
    },
    // 7、违章记录  提交
    *commitEndorse({ payload, callback }, { call }) {
      const response = yield call(commitEndorse, payload);
      const { code, data, message } = response;
      const content = `提交${message}`;
      if (code !== 0) return;
      prompt({ content });
      callback && callback(true);
    },



    /**
     * 设置表单参数
     */
    *allValus({ payload }, { _, put }) {
      yield put({
        type: 'saveAllValus',
        payload,
      });
    },


    *selectFileList({ payload, callback }, { call }) {
      const response = yield call(selectFileList, payload);
      const { code, message, data } = response;
      if (code === 0) {
        if (callback) callback(data);
      }
    },
  },

  //Reducer 是 Action 处理器，用来处理同步操作，
  reducers: {
    //设置表单参数
    saveAllValus(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    oilCard(state, { payload }) {
      return {
        ...state,
        oilCardList: payload.oilCardList,
      };
    },
    detail(state, { payload }) {
      let param = '';
      for (let k in payload) {
        param = k;
      }
      return {
        ...state,
        [param]: { ...state[param], ...payload[param] },
      };
    },
  },
};
